package cn.iocoder.yudao.module.member.service.usercommission;

import cn.iocoder.yudao.module.member.dal.dataobject.userpromotionrecords.UserPromotionRecordsDO;
import cn.iocoder.yudao.module.member.enums.usercommission.InviterTypeEnum;
import cn.iocoder.yudao.module.member.service.userpromotionrecords.UserPromotionRecordsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.member.controller.admin.usercommission.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercommission.UserCommissionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.usercommission.UserCommissionMapper;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 佣金 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCommissionServiceImpl implements UserCommissionService {

    @Resource
    private UserCommissionMapper userCommissionMapper;
    @Resource
    private UserPromotionRecordsService userPromotionRecordsService;
    @Resource
    private CommissionService commissionService;

    @Override
    public Long createUserCommission(UserCommissionSaveReqVO createReqVO) {
        // 插入
        UserCommissionDO userCommission = BeanUtils.toBean(createReqVO, UserCommissionDO.class);
        userCommissionMapper.insert(userCommission);
        // 返回
        return userCommission.getId();
    }

    @Override
    public void addUserCommission(Long inviteeUserId, Long payPackageOrderId, Integer recharge) {
        UserCommissionSaveReqVO createReqVOFirst = new UserCommissionSaveReqVO();
        UserPromotionRecordsDO userPromotionRecordsDOFirst = userPromotionRecordsService.getByInviteeUserId(inviteeUserId);
        if (Objects.isNull(userPromotionRecordsDOFirst)) {
            return;
        }
        createReqVOFirst.setUserId(userPromotionRecordsDOFirst.getInviterUserId());
        createReqVOFirst.setInviterType(InviterTypeEnum.FIRST.getCode());
        createReqVOFirst.setFirstInviteeUserId(userPromotionRecordsDOFirst.getInviteeUserId());
        createReqVOFirst.setPayPackageOrderId(payPackageOrderId);
        createReqVOFirst.setRecharge(recharge);
        createReqVOFirst.setCommission(commissionService.calculateStepCommission(firstTotalRechargeByUserId(userPromotionRecordsDOFirst.getInviterUserId()), recharge));
        createUserCommission(createReqVOFirst);

        UserCommissionSaveReqVO createReqVOSecond = new UserCommissionSaveReqVO();
        UserPromotionRecordsDO userPromotionRecordsDOSecond = userPromotionRecordsService.getByInviteeUserId(userPromotionRecordsDOFirst.getInviterUserId());
        if (Objects.isNull(userPromotionRecordsDOSecond)) {
            return;
        }
        createReqVOSecond.setUserId(userPromotionRecordsDOSecond.getInviterUserId());
        createReqVOSecond.setInviterType(InviterTypeEnum.SECOND.getCode());
        createReqVOSecond.setFirstInviteeUserId(userPromotionRecordsDOFirst.getInviterUserId());
        createReqVOSecond.setSecondInviteeUserId(userPromotionRecordsDOFirst.getInviteeUserId());
        createReqVOSecond.setPayPackageOrderId(payPackageOrderId);
        createReqVOSecond.setRecharge(recharge);
        createReqVOSecond.setCommission(commissionService.calculateFixedRatioCommission(recharge));
        createUserCommission(createReqVOSecond);
    }

    @Override
    public void updateUserCommission(UserCommissionSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCommissionExists(updateReqVO.getId());
        // 更新
        UserCommissionDO updateObj = BeanUtils.toBean(updateReqVO, UserCommissionDO.class);
        userCommissionMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCommission(Long id) {
        // 校验存在
        validateUserCommissionExists(id);
        // 删除
        userCommissionMapper.deleteById(id);
    }

    private void validateUserCommissionExists(Long id) {
        if (userCommissionMapper.selectById(id) == null) {
            throw exception(USER_COMMISSION_NOT_EXISTS);
        }
    }

    @Override
    public UserCommissionDO getUserCommission(Long id) {
        return userCommissionMapper.selectById(id);
    }

    @Override
    public PageResult<UserCommissionDO> getUserCommissionPage(UserCommissionPageReqVO pageReqVO) {
        return userCommissionMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer totalCommissionByUserId(Long userId) {
        Integer commission = userCommissionMapper.totalCommissionByUserId(userId);
        return Objects.isNull(commission) ? 0 : commission;
    }

    @Override
    public Integer firstTotalRechargeByUserId(Long userId) {
        Integer recharge = userCommissionMapper.firstTotalRechargeByUserId(userId);
        return Objects.isNull(recharge) ? 0 : recharge;
    }

    @Override
    public Integer firstTotalRecharge(Long firstInviteeUserId) {
        Integer recharge = userCommissionMapper.firstTotalRecharge(firstInviteeUserId);
        return Objects.isNull(recharge) ? 0 : recharge;
    }

    @Override
    public Integer secondTotalRecharge(Long firstInviteeUserId) {
        Integer recharge = userCommissionMapper.secondTotalRecharge(firstInviteeUserId);
        return Objects.isNull(recharge) ? 0 : recharge;
    }

}